// pages/scheme/scheme.js
"use strict"
import mode from "../../utils/mode.js"

const wxParse = require('../../wxParse/wxParse.js')
Page({

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage () {   //分享好友
    //     this.setData({
    //         isShare:1,
    //       })
    //     return {
    //         title:this.data.shareText,
    //         path: `/pages/shareIndex/shareIndex?share_id=${this.data.userMessage.id}&userName=${this.data.userMessage.user_nickname}`,
    //         imageUrl: 'https://2am-project.cn/img/20220325/shareImg.png',   //分享图片
    //     }
    // },
    /**
     * 页面的初始数据
     */
    data: {
        goodsList:[],
        shareText:'邀你在线肌肤问诊，点...',  //分享内容
        list: {},
        userId: '',
        choselist: [],
        userMessage:{},
        temIds:['Wzq0Q8OpleP_KgHNxStTKJ6-8NdvRvqgJnioZ01w7bg','IKdsAyES45qURTGEQMaHqLRq-fpoaU83kNRtz3TcC78'],
        isShare:0,  //是否进行分享
        showShare:false,  //分享成功展示
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        mode.proposalRecommend(options.id).then(item => {
            let product = item.data.product
            Array.isArray(product) && product.forEach(x => {
                x.selected = x.goods_can_sell_number>0?true:false
                x.num = 1
                var uInfo = that.data.userMessage
                if (uInfo && uInfo.consumption_discount && uInfo.is_member>0) {
                    let oldP = x.retail_price * uInfo.consumption_discount / 10 * 1
                    x.vip_price = oldP.toFixed(2)
                }
            })
            this.setData({
                goodsList:product,
                list: item.data
            })
            if (item.data.experience_product.goods_content) {
                var parseContent = wxParse.wxParse('article', 'html', item.data.experience_product.goods_content, that, 5)
            }
        })
    },
    // getGoods(ids){
    //     let that = this
    //     wx.$api.goodsLists({
    //         goods_ids:ids
    //       }).then(res=>{
    //         if (res.code) {
    //           let data = res.data
    //           if(data.length>0){
    //             data.forEach(x => {
    //                 x.selected = x.goods_can_sell_number>0?true:false
    //                 x.num = 1
    //                 var uInfo = that.data.userMessage
    //                 if (uInfo && uInfo.consumption_discount>0 &&uInfo.is_member>0) {
    //                     let oldP = x.retail_price * uInfo.consumption_discount / 10 * 1
    //                     x.vip_price = oldP.toFixed(2)
    //                 }
    //             })
    //             that.setData({
    //                 goodsList: data
    //             })
    //           }else{
    //             that.setData({
    //                 goodsList: that.data.list.product
    //             })
    //           }
    //         }else{
    //             that.setData({
    //                 goodsList: that.data.list.product
    //             })
    //         }
    //     })
    // },
    setInfo(){// 订阅消息
        let that = this
        this.setData({
          showShare:false,
          isShare:0
        })
        let shareData={
          user_id:that.data.userMessage.id,
          type:3,
          other_type:'',
        }
        wx.requestSubscribeMessage({
            tmplIds: that.data.temIds,
            success (res) {
              let arr = []
                if(res[that.data.temIds[0]]=='accept'){//拒绝
                  arr.push('1')
                }
                if(res[that.data.temIds[1]]=='accept'){
                  arr.push('2')
                }
                // else if(res[that.data.temIds[2]]=='accept'){
                  // arr.push('3')
                // }else if(res[that.data.temIds[3]]=='accept'){  //允许
                  // arr.push('4')
                // }
                if(arr.length>0){
                  shareData.other_type = arr.join(',')
                  that.sendSetYue(shareData)
                }
            },
            fail(error){
              console.log(error,'123失败');
            }
          })
      },
      sendSetYue(data){
        wx.$api.sendTemplate(data).then(res=>{
                if(res.code>0){
                }else{
                    wx.showToast({
                      title: res.msg,
                      icon:'none'
                    })
                }
        })
    },
    goDetail (e) {
        let { goodid } = e.currentTarget.dataset
        if (goodid) {
            wx.navigateTo({
                url: '/pages/product-details/product-details?goodId=' + goodid
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let info = wx.getStorageSync('uinfo')
        if (info.user.id) {
            let n = info.user.user_nickname || ''
          let title =  n + '邀你在线肌肤问诊，点...'
            this.setData({
                shareText: title,
                userId: info.user.id,
                userMessage: info.user
            })
        }
        // if(this.data.isShare>0){
        //     this.setData({
        //       showShare:true
        //     })
        // }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    goShare(){
        wx.navigateTo({
           url: '/pages/sharePage/sharePage',
        })
    },
    toGet () {
        var that = this
        if (!that.data.userMessage || !that.data.userMessage.id) {
            wx.navigateTo({
                url: '/pages/register/register',
            })
        } else if(that.data.userMessage.test_goods<1) {
            if (!that.data.list.experience_product) {
                wx.showToast({
                    title: '此商品已下架',
                    icon: 'none',
                    duration: 2000
                })
            } else {
                let goods_id = that.data.list.experience_product.id
                    
                wx.navigateTo({
                    url: '/pages/settlement/settlement?is_test=1&testGoodId=' + goods_id
                })
            }
        }else{
            wx.showToast({ title: '用户不可重复领取体验装！', icon: 'none' })
        }

    },
    selectList (e) {
        var that = this
        let id = e.currentTarget.dataset.index
        let carts = this.data.goodsList
        const selected = carts[id].selected
        carts[id].selected = !selected
        that.setData({
            goodsList: carts
        })
    },
    async addCar () {
        let arr = []
        let cars = this.data.goodsList
        for (var i = 0; i < cars.length; i++) {
            if (cars[i].selected) {
                arr.push({ goods_id: cars[i].id, number: cars[i].num })
            }
        }
        if (!arr.length) {
            wx.showToast({
                title: '请先勾选产品！',
                icon: 'none'
            })
            return
        }
        wx.showLoading({ mask: true })
        let reqData = {
            user_id: this.data.userId,
            goods_info: JSON.stringify(arr),
        }
        let { code, data } = await mode.cartAdd(reqData)
        if (code == 1) {
            wx.hideLoading()
            wx.showToast({
                title: '商品添加成功！',
                icon: 'none',
                duration: 2000
            })
            setTimeout(() => {
                wx.switchTab({
                    url: '/pages/exclusive/exclusive',
                })
            }, 500)
        } else {
            wx.hideLoading()
        }
    },
    onChange (e) {
        // console.log(`%c✅e=> `, 'color:#ff7b54;font-size:16px;', e)
        let { index } = e.target.dataset
        let product = this.data.goodsList
        product.forEach(x => {
            if (x.id === index) {
                x.num = e.detail
            }
        })
        this.setData({
            goodsList: product
        })
    }
})